﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SecurityApi.Business;
using SecurityApi.DataAccess;
using System.Configuration;

namespace SecurityAdmin.Web
{
    public class BaseController : Controller
    {
        private const string _selectedUserKey = "SELECTED-USER";
        private const string _selectedApplicationKey = "SELECTED-APPLICATION";
        private const string _selectedEnterpriseKey = "SELECTED-ENTERPRISE";

        public int EnterpriseId
        {
            get { return GetEnterpriseId(); }
        }

        public EnterpriseUser SelectedUser
        {
            get { return GetCurrentUser(); }
            set { SetCurrentUser(value); }
        }

        public SecurityApi.Model.Application SelectedApplication
        {
            get { return GetCurrentApplication(); }
            set { SetCurrentApplication(value); }
        }

        public SecurityApi.Model.Enterprise SelectedEnterprise
        {
            get { return GetCurrentEnterprise(); }
        }


        #region Current User
        protected void SetCurrentUser(EnterpriseUser user)
        {
            Session[_selectedUserKey] = user;
        }

        protected EnterpriseUser GetCurrentUser()
        {
            EnterpriseUser user = null;
            if (null != Session[_selectedUserKey])
            {
                user = Session[_selectedUserKey] as EnterpriseUser;
            }

            return user;
        }
        #endregion

        #region Application Stuff
        protected void SetCurrentApplication(SecurityApi.Model.Application application)
        {
            Session[_selectedApplicationKey] = application;
        }

        protected SecurityApi.Model.Application GetCurrentApplication()
        {
            SecurityApi.Model.Application app = null;
            if (null != Session[_selectedApplicationKey])
            {
                app = Session[_selectedApplicationKey] as SecurityApi.Model.Application;
            }

            return app;
        }

        protected void SelectDefaultApplication()
        {
            SecurityApi.Model.Enterprise enterprise = GetCurrentEnterprise();

            if (null != enterprise)
            {
                ApplicationManager mgr = new ApplicationManager();
                List<SecurityApi.Model.Application> apps = mgr.GetEnterpriseApplications(enterprise.Id);
                if (apps.Count > 0)
                {
                    SelectedApplication = apps[0];
                }
                else
                {
                    SelectedApplication = null;
                }
            }
            else
            {
                SelectedApplication = null;
            }
        }
        #endregion


        #region Enterprise Stuff
        
        /// <summary>
        ///When enterprise selection is changed, we need to change the respective application stuff also
        /// </summary>
        /// <param name="enterprise"></param>
        protected void SetCurrentEnterprise(SecurityApi.Model.Enterprise enterprise)
        {
            if (null != enterprise)
            {
                ApplicationManager mgr = new ApplicationManager();
                List<SecurityApi.Model.Application> apps = mgr.GetEnterpriseApplications(enterprise.Id);

                if (apps.Count > 0)
                {
                    SetCurrentApplication(apps[0]);
                }
                else
                {
                    //No application is available
                    SetCurrentApplication(null);
                }

                Session[_selectedEnterpriseKey] = enterprise;
            }
            else
            {
                Debug.Assert(true, "Enterprise Cannot be null");
            }
        }

        protected SecurityApi.Model.Enterprise GetCurrentEnterprise()
        {
            SecurityApi.Model.Enterprise enterprise = null;
            if (null != Session[_selectedEnterpriseKey])
            {
                enterprise = Session[_selectedEnterpriseKey] as SecurityApi.Model.Enterprise;
            }

            return enterprise;
        }

        protected void SelectDefaultEnterprise()
        {
            EnterpriseManager mgr = new EnterpriseManager();
            SecurityApi.Model.Enterprise enterprise = mgr.GetEnterpriseById(1);
            SetCurrentEnterprise(enterprise);
        }

        #endregion

        protected int GetEnterpriseId()
        {
            int enterpriseId = 1;
            string strId = ConfigurationManager.AppSettings["EnterpriseId"].ToString();
            if (!int.TryParse(strId, out enterpriseId))
            {
                enterpriseId = 1;
            }
            return enterpriseId;
        }
    }
}